{% extends "mainapp/base.html" %}

{% load static %}
{% load bootstrap %}

{% block title %} 批次产品信息管理 {% endblock %}

{% block content %}
    <h4 class="mb-4">提交生成报告</h4>
    <div class="row">
        <div class="col-10">
            <form method="post" action="{% url 'do_auto_generate_report' product.id %}" novalidate>
                <input type="hidden" name="random_string" value="{{ random_string }}">

                {% csrf_token %}
                <table class="table table-bordered">
                    <tr>
                        <th>生产批次号：</th>
                        <td>
                            {{ form.h_lot_number }}
                            <input type="hidden" name="h_lot_number" value="{{ form.h_lot_number.value }}">
                        </td>

                        <th>产品类型：</th>
                        <td>
                            {{ form.h_product_type }}
                            <input type="hidden" name="h_product_type" value="{{ form.h_product_type.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>报告编号：</th>
                        <td>
                            {{ form.report_number }}
                            <input type="hidden" name="report_number" value="{{ form.report_number.value }}">
                        </td>

                        <th>报告日期：</th>
                        <td>
                            {{ form.report_date }}
                            <input type="hidden" name="report_date" value="{{ form.report_date.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>客户名称：</th>
                        <td>
                            {{ form.customer_name }}
                            <input type="hidden" name="customer_name" value="{{ form.customer_name.value }}">
                        </td>

                        <th>订单号码：</th>
                        <td>
                            {{ form.order_number }}
                            <input type="hidden" name="order_number" value="{{ form.order_number.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>出货批次号：</th>
                        <td>
                            {{ form.production_lot_number }}
                            <input type="hidden" name="production_lot_number"
                                   value="{{ form.production_lot_number.value }}">
                        </td>

                        <th>规格型号：</th>
                        <td>
                            {{ form.specification_model }}
                            <input type="hidden" name="specification_model"
                                   value="{{ form.specification_model.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>批量：</th>
                        <td>
                            {{ form.lot_quantity }}
                            <input type="hidden" name="lot_quantity" value="{{ form.lot_quantity.value }}">
                        </td>

                        <th>方向角度：</th>
                        <td>
                            {{ form.direction_angle }}
                            <input type="hidden" name="direction_angle" value="{{ form.direction_angle.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>投料角度：</th>
                        <td>
                            {{ form.feeding_angle }}
                            <input type="hidden" name="feeding_angle"
                                   value="{{ form.feeding_angle.value }}">
                        </td>

                        <th>频率范围：</th>
                        <td>
                            {{ form.frequency_range }}
                            <input type="hidden" name="frequency_range"
                                   value="{{ form.frequency_range.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>频率测量间隙：</th>
                        <td>
                            {{ form.frequency_measurement_gap }}
                            <input type="hidden" name="frequency_measurement_gap"
                                   value="{{ form.frequency_measurement_gap.value }}">
                        </td>

                        <th>出货产品料号：</th>
                        <td>
                            {{ form.customer_material_number }}
                            <input type="hidden" name="customer_material_number"
                                   value="{{ form.customer_material_number.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>产品尺寸：</th>
                        <td>
                            {{ form.product_size }}
                            <input type="hidden" name="product_size"
                                   value="{{ form.product_size.value }}">
                        </td>

                        <th>外观抽样数：</th>
                        <td>
                            {{ form.appearance_sample_quantity }}
                            <input type="hidden" name="appearance_sample_quantity"
                                   value="{{ form.appearance_sample_quantity.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>Ma-ACC：</th>
                        <td>
                            {{ form.ma_accept }}
                            <input type="hidden" name="ma_accept"
                                   value="{{ form.ma_accept.value }}">
                        </td>

                        <th>Ma-REJ：</th>
                        <td>
                            {{ form.ma_reject }}
                            <input type="hidden" name="ma_reject"
                                   value="{{ form.ma_reject.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>△F规格值：</th>
                        <td>
                            {{ form.frequency_diff_limit }}
                            <input type="hidden" name="frequency_diff_limit"
                                   value="{{ form.frequency_diff_limit.value }}">
                        </td>

                        <th>△F管控值：</th>
                        <td>
                            {{ form.frequency_diff_control }}
                            <input type="hidden" name="frequency_diff_control"
                                   value="{{ form.frequency_diff_control.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>频率偏移度：</th>
                        <td colspan="3">
                            {{ form.frequency_offset_degree }}
                            <input type="hidden" name="frequency_offset_degree"
                                   value="{{ form.frequency_offset_degree.value }}">
                        </td>
                    </tr>
                    <tr>
                        <th>尺寸数据地址：</th>
                        <td colspan="3">{{ form.size_data_address }}</td>
                    </tr>
                    <tr>
                        <th>直角度数据地址：</th>
                        <td colspan="3">{{ form.angle_data_address }}</td>
                    </tr>
                    <tr>
                        <th>频率数据地址：</th>
                        <td colspan="3">{{ form.frequency_data_address }}</td>
                    </tr>
                    <tr>
                        <th>△F数据地址：</th>
                        <td colspan="3">{{ form.df_data_address }}</td>
                    </tr>
                    <tr>
                        <td colspan="4" class="text-right">
                            <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                                <button type="submit" class="btn btn-primary">生成报告</button>
                                <a href="{% url 'product_batch_info_list' %}" class="btn btn-secondary">返回</a>
                            </div>
                        </td>
                    </tr>
                    {% if error_msg %}
                        <tr>
                            <th colspan="4">
                                <div class="alert alert-danger">出现错误：{{ error_msg|safe }}</div>
                            </th>
                        </tr>
                    {% endif %}
                </table>
            </form>
        </div>
        <div class="col-6">
            {% if form.errors %}
                <div class="alert alert-danger mt-3">
                    <strong>提交错误:</strong>
                    <ul>
                        {% for field, errors in form.errors.items %}
                            {% for error in errors %}
                                <li>{{ field }}: {{ error }}</li>
                            {% endfor %}
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
        </div>
    </div>

    <div class="row">
        <div class="col-10">
            <h4 class="mt-5">已生成报告列表</h4>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>文件名</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for file in download_files %}
                    <tr id="file-{{ file.id }}">
                        <td>{{ file.file_path }}</td>
                        <td>{{ file.created_at|date:"Y-m-d H:i:s" }}</td>
                        <td>
                            <a href="{{ MEDIA_URL }}{{ file.file_path }}" class="btn btn-primary btn-sm">下载</a>
                            <button class="btn btn-danger btn-sm delete-file" data-file-id="{{ file.id }}">删除</button>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}

{% block jscodes %}
    <script>
        $(document).ready(function () {
            $(".delete-file").click(function () {
                var fileId = $(this).data("file-id");
                var $row = $("#file-" + fileId);
                if (confirm("确定要删除这个文件吗？")) {
                    $.ajax({
                        url: "{% url 'delete_download_file' %}",
                        type: "POST",
                        headers: {
                            "X-CSRFToken": "{{ csrf_token }}"
                        },
                        data: JSON.stringify({file_id: fileId}),
                        contentType: "application/json",
                        success: function (data) {
                            if (data.status === 'success') {
                                $row.remove();
                            } else {
                                alert(data.message);
                            }
                        }
                    });
                }
            });
        });
    </script>
{% endblock %}